﻿namespace System
{
    using System.Runtime.CompilerServices;
    using System.Threading;

    public class AsyncAction : IAsyncAction
    {
        private bool AutoComplete;
        private EventHandler _Completed;
        private string taskName;

        public event EventHandler Completed
        {
            add
            {
                EventHandler handler2;
                EventHandler completed = this._Completed;
                do
                {
                    handler2 = completed;
                    EventHandler handler3 = (EventHandler)Delegate.Combine(handler2, value);
                    completed = Interlocked.CompareExchange<EventHandler>(ref this._Completed, handler3, handler2);
                }
                while (completed != handler2);
            }
            remove
            {
                EventHandler handler2;
                EventHandler completed = this._Completed;
                do
                {
                    handler2 = completed;
                    EventHandler handler3 = (EventHandler)Delegate.Remove(handler2, value);
                    completed = Interlocked.CompareExchange<EventHandler>(ref this._Completed, handler3, handler2);
                }
                while (completed != handler2);
            }
        }

        public AsyncAction()
        {
            this.AutoComplete = false;
            this.taskName = string.Empty;
            this.ActionResult = new System.ActionResult();
        }

        public AsyncAction(string name)
            : this()
        {
            this.TaskName = name;
        }

        public virtual void Execute()
        {
            if (this.Action != null)
            {
                this.ActionResult.StartTime = DateTime.Now;
                this.Action();
                if (!this.AutoComplete)
                {
                    this.OnCompleted();
                }
            }
        }

        public void OnCompleted()
        {
            EventHandler completed = this._Completed;
            if (completed != null)
            {
                completed(this, EventArgs.Empty);
            }
        }

        public void SetAction(System.Action action)
        {
            this.SetAction(action, true);
        }
        /// <summary>
        /// 设置要执行的操作
        /// </summary>
        /// <param name="action">操作</param>
        /// <param name="autoComplete">是否自动完成</param>

        public void SetAction(System.Action action, bool autoComplete)
        {
            this.Action = action;
            this.AutoComplete = autoComplete;
        }

        private System.Action Action { get; set; }
        /// <summary>
        /// 当前任务的结果
        /// </summary>

        public System.ActionResult ActionResult { get; set; }
        /// <summary>
        /// 前置任务的结果，添加这个功能目的是，可能多个任务之间互相有所依赖，例如某个任务要根据前面任务的情况决定是否执行
        /// </summary>
        public System.ActionResult PreActionResult { get; set; }
        /// <summary>
        /// 任务名称
        /// </summary>

        public string TaskName
        {
            get
            {
                return this.taskName;
            }
            set
            {
                this.taskName = value;
                this.ActionResult.TaskName = value;
            }
        }
    }
}

